﻿$(document).ready(function () {
    //$("#datepicker").datepick({ dateFormat: 'dd-mm-yyyy' });
    $(".numeric").numeric();
    $.getJSON("/Home/GetNoiDen/", { isTrongNuoc: $("input[name=IsTrongNuoc]").filter(":checked").val() }, function (data) {
        //var a=JSON.parse(
        //$("#NoiDenList").val(data);
        //jQuery.pa
        var str = "";
        var lc = "";
        var noiDen = $("#NoiDenRequest").val();
        $.each(data, function (index, d) {
            if (d != "" && d != null) {
                if (d.TinhQuocGia != lc && index != 0) {
                    if (lc != "") {
                        str += "</optgroup>";
                    }
                    str += "<optgroup label='" + d.TinhQuocGia + "'>";
                    lc = d.TinhQuocGia;
                }
                str += "<option value='" + d.TenNoiDen + "'";
                if (noiDen == d.TenNoiDen) {
                    str += " selected=selected";
                }
                str+=">" + d.TenNoiDen + "</option>";
            }
        });
        if (str != "") {
            str += "</optgroup>";
        }
        $("#NoiDen").html(str);
    });

    $.getJSON("/Home/GetGia/", { isTrongNuoc: $("input[name=IsTrongNuoc]").filter(":checked").val() }, function (data) {
        var str = "";
        var gia = $("#GiaRequest").val();
        $.each(data, function (index, d) {
            if (d != "" && d != null) {
                str += "<option value='" + d.Code + "'";
                if (gia!="" && gia == d.Code) {
                    str += " selected=selected";
                }
                str+=">" + d.Name + "</option>";
            }
        });
        $("#Gia").html(str);
    });
});

function demo() {
    $.getJSON("/Home/GetNoiDen/", { isTrongNuoc: $("input[name=IsTrongNuoc]").filter(":checked").val() }, function(data) {
        //var a=JSON.parse(
        //$("#NoiDenList").val(data);
        //jQuery.pa
        $("#NoiDenRequest").val("");
        $("#GiaRequest").val("");
        var str = "";
        var lc = "";
        $.each(data, function(index, d) {
            if (d != "" && d != null) {
                if (d.TinhQuocGia != lc && index != 0) {
                    if (lc != "") {
                        str += "</optgroup>";
                    }
                    str += "<optgroup label='" + d.TinhQuocGia + "'>";
                    lc = d.TinhQuocGia;
                }
                str += "<option value='" + d.TenNoiDen + "'>" + d.TenNoiDen + "</option>";
            }
        });
        if (str != "") {
            str += "</optgroup>";
        }
        $("#NoiDen").html(str);
    });

    $.getJSON("/Home/GetGia/", { isTrongNuoc: $("input[name=IsTrongNuoc]").filter(":checked").val() }, function(data) {
        var str = "";
        $.each(data, function(index, d) {
            if (d != "" && d != null) {
                str += "<option value='" + d.Code + "'>" + d.Name + "</option>";
            }
        });
        $("#Gia").html(str);
    });
}

//Người lớn:0, Trẻ em:1, Trẻ nhỏ:2
//Việt Nam:0, Việt Kiều:1, Nước Ngoài:2
function TaoKhachHang(e) {
    var loaiKhachList = JSON.parse($("#LoaiKhachList").val());
    var tbDanhSach = $("#tbDanhSach");
    var soNguoiLon = 0;
    var soTreEm = 0;
    var soTreNho = 0;
    var tongSoKhach = 0;
    if($("#TongSoKhach").val()!="") {
        tongSoKhach = parseInt($("#TongSoKhach").val());
    }
    if ($("#SoNguoiLon").val()!="") {
        soNguoiLon = parseInt($("#SoNguoiLon").val());
    }
    if ($("#SoTreEm").val() != "") {
        soTreEm = parseInt($("#SoTreEm").val());
    }
    if ($("#SoTreNho").val() != "") {
        soTreNho = parseInt($("#SoTreNho").val());
    }
    $("#TongSoKhach").val(soNguoiLon + soTreNho + soTreEm);
    for(var i=1;i<=tongSoKhach;i++) {
        $("#" + i).remove();
    }
    $("#TongSoKhachModel").val(soNguoiLon + soTreNho + soTreEm);
    $("#SoNguoiLonModel").val(soNguoiLon);
    $("#SoTreEmModel").val(soTreEm);
    $("#SoTreNhoModel").val(soTreNho);
    tongSoKhach = $("#TongSoKhach").val();
    var str = "";
    i = 1;
    for(var j=1;j<=soNguoiLon;j++) {
        str += "<tr id='" + i + "'>";
        str += "<td>";
        str += "<input type='text' name='HoTen" + i + "' id='HoTen" + i + "' maxlength='100' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<input type='text' name='NgaySinh" + i + "' id='NgaySinh" + i + "' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<input type='text' name='DiaChi" + i + "' maxlength='150' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<select name='GioiTinh"+i+"'>" +
            "<option value='0'>Nữ</option>" +
            "<option value='1'>Nam</option>" +
            "</select>";
        str += "</td>";
        str += "<td>";
        str += "<select name='LoaiKhach" + i + "' id='LoaiKhach" + i + "' onchange='TinhTien(this.id);'>";
        $.each(loaiKhachList, function (index, d) {
            str += "<option value='" + d.Code + "'>" + d.Name + "</option>";
        });
        str += "</select>";
        str += "</td>";
        str += "<td>";
        str += "Người lớn";
        str += "<input type='hidden' name='DoTuoi" + i + "' id='DoTuoi" + i + "' value='0'/>";
        str += "</td>";
        str += "<td>";
        str += "<select name='PhongDon" + i + "' id='PhongDon" + i + "' onchange='TinhTien(this.id);'>" +
            "<option value='0'>Không</option>" +
            "<option value='1'>Có</option>" +
            "</select>";
        str += "</td>";
        str += "<td>";
        str += "<input type='hidden' name='Gia" + i + "' id='Gia" + i + "'/>" +
                "<b id='TriGia" + i + "'></b>";
        str += "</td>";
        str += "</tr>";
        i++;
    }
    for (j = 1; j <= soTreEm; j++) {
        str += "<tr id='" + i + "'>";
        str += "<td>";
        str += "<input type='text' name='HoTen" + i + "' id='HoTen"+i+"' maxlength='100' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<input type='text' name='NgaySinh" + i + "' id='NgaySinh" + i + "' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<input type='text' name='DiaChi" + i + "' maxlength='150' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<select name='GioiTinh" + i + "'>" +
            "<option value='0'>Nữ</option>" +
            "<option value='1'>Nam</option>" +
            "</select>";
        str += "</td>";
        str += "<td>";
        str += "<select name='LoaiKhach" + i + "' id='LoaiKhach" + i + "' onchange='TinhTien(this.id);'>";
        $.each(loaiKhachList, function (index, d) {
            str += "<option value='" + d.Code + "'>" + d.Name + "</option>";
        });
        str += "</select>";
        str += "</td>";
        str += "<td>";
        str += "Trẻ em";
        str += "<input type='hidden' name='DoTuoi" + i + "' id='DoTuoi" + i + "' value='1'/>";
        str += "</td>";
        str += "<td>";
        str += "<select name='PhongDon" + i + "' id='PhongDon" + i + "' onchange='TinhTien(this.id);'>" +
            "<option value='0'>Không</option>" +
            "<option value='1'>Có</option>" +
            "</select>";
        str += "</td>";
        str += "<td>";
        str += "<input type='hidden' name='Gia" + i + "' id='Gia" + i + "'/>" +
                "<b id='TriGia" + i + "'></b>";
        str += "</td>";
        str += "</tr>";
        i++;
    }
    for (j = 1; j <= soTreNho; j++) {
        str += "<tr id='" + i + "'>";
        str += "<td>";
        str += "<input type='text' name='HoTen" + i + "' id='HoTen" + i + "' maxlength='100' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<input type='text' name='NgaySinh" + i + "' id='NgaySinh"+i+"' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<input type='text' name='DiaChi" + i + "' maxlength='150' style='width: auto'/>";
        str += "</td>";
        str += "<td>";
        str += "<select name='GioiTinh" + i + "'>" +
            "<option value='0'>Nữ</option>" +
            "<option value='1'>Nam</option>" +
            "</select>";
        str += "</td>";
        str += "<td>";
        str += "<select name='LoaiKhach" + i + "' id='LoaiKhach" + i + "' onchange='TinhTien(this.id);'>";
        $.each(loaiKhachList, function (index, d) {
            str += "<option value='" + d.Code + "'>" + d.Name + "</option>";
        });
        str += "</select>";
        str += "</td>";
        str += "<td>";
        str += "Trẻ nhỏ";
        str += "<input type='hidden' name='DoTuoi" + i + "' id='DoTuoi"+i+"' value='2'/>";
        str += "</td>";
        str += "<td>";
        str += "<select name='PhongDon" + i + "' id='PhongDon"+i+"' onchange='TinhTien(this.id);'>" +
            "<option value='0'>Không</option>" +
            "<option value='1'>Có</option>" +
            "</select>";
        str += "</td>";
        str += "<td>";
        str+="<input type='hidden' name='Gia"+i+"' id='Gia"+i+"'/>" +
                "<b id='TriGia"+i+"'></b>";
        str += "</td>";
        str += "</tr>";
        i++;
    }
    tbDanhSach.append(str);
    for (i = 1; i <= tongSoKhach; i++) {
        TinhTien(i.toString());
    }
}

Number.prototype.formatMoney = function (decPlaces, thouSeparator, decSeparator) {
    var n = this,
    decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
    decSeparator = decSeparator == undefined ? "." : decSeparator,
    thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
    sign = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
    j = (j = i.length) > 3 ? j % 3 : 0;
    return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
};

//Người lớn:0, Trẻ em:1, Trẻ nhỏ:2
//Việt Nam:0, Việt Kiều:1, Nước Ngoài:2
function TinhTien(e) {
    var giaTourCoBanNguoiLonVietNam = parseInt($("#GiaTourCoBanNguoiLonVietNam").val());
    var giaTourCoBanNguoiLonVietKieu = parseInt($("#GiaTourCoBanNguoiLonVietKieu").val());
    var giaTourCoBanNguoiLonNuocNgoai = parseInt($("#GiaTourCoBanNguoiLonNuocNgoai").val());
    var giaTourCoBanTreEmVietNam = parseInt($("#GiaTourCoBanTreEmVietNam").val());
    var giaTourCoBanTreEmVietKieu = parseInt($("#GiaTourCoBanTreEmVietKieu").val());
    var giaTourCoBanTreEmNuocNgoai = parseInt($("#GiaTourCoBanTreEmNuocNgoai").val());
    var giaTourCoBanTreNhoVietNam = parseInt($("#GiaTourCoBanTreNhoVietNam").val());
    var giaTourCoBanTreNhoVietKieu = parseInt($("#GiaTourCoBanTreNhoVietKieu").val());
    var giaTourCoBanTreNhoNuocNgoai = parseInt($("#GiaTourCoBanTreNhoNuocNgoai").val());

    var giaPhuThuPhongDonVietNam = parseInt($("#GiaPhuThuPhongDonVietNam").val());
    var giaPhuThuPhongDonVietKieu = parseInt($("#GiaPhuThuPhongDonVietKieu").val());
    var giaPhuThuPhongDonNuocNgoai = parseInt($("#GiaPhuThuPhongDonNuocNgoai").val());
    var so = e.replace("PhongDon", "");
    so = so.replace("LoaiKhach", "");
    var phongDon = $("#PhongDon" + so).val();
    var loaiKhach = $("#LoaiKhach" + so).val();
    var doTuoi = $("#DoTuoi" + so).val();
    var gia = 0;
    if (loaiKhach == 0) {
        if (doTuoi == 0) {
            gia += giaTourCoBanNguoiLonVietNam;
        }
        else if (doTuoi = 1) {
            gia += giaTourCoBanTreEmVietNam;
        }
        else if (doTuoi = 2) {
            gia += giaTourCoBanTreNhoVietNam;
        }
        if (phongDon == 1) {
            gia += giaPhuThuPhongDonVietNam;
        }
    }
    else if (loaiKhach == 1) {
        if (doTuoi == 0) {
            gia += giaTourCoBanNguoiLonVietKieu;
        }
        else if (doTuoi = 1) {
            gia += giaTourCoBanTreEmVietKieu;
        }
        else if (doTuoi = 2) {
            gia += giaTourCoBanTreNhoVietKieu;
        }
        if (phongDon == 1) {
            gia += giaPhuThuPhongDonVietKieu;
        }
    }
    else if (loaiKhach == 2) {
        if (doTuoi == 0) {
            gia += giaTourCoBanNguoiLonNuocNgoai;
        }
        else if (doTuoi = 1) {
            gia += giaTourCoBanTreEmNuocNgoai;
        }
        else if (doTuoi = 2) {
            gia += giaTourCoBanTreNhoNuocNgoai;
        }
        if (phongDon == 1) {
            gia += giaPhuThuPhongDonNuocNgoai;
        }
    }
    $("#TriGia" + so).html(gia.formatMoney(0, ',', '.'));
    $("#Gia" + so).val(gia);
}

function CheckDataBeforeChapNhan() {
    var soHanhKhach = $("#TongSoKhachModel").val();
    if (soHanhKhach == null || soHanhKhach == 0) {
        alert("Xin nhập ít nhất một khách hàng đi tour");
        return false;
    }
    for (var i = 1; i <= soHanhKhach; i++) {
        var hoTen = $("#HoTen" + i).val();
        var ngaySinh=$("#NgaySinh"+i).val();
        if (hoTen==null || hoTen.toString().trim() == "") {
            alert("Khách hàng thứ " + i + " chưa được nhập họ tên");
            return false;
        }
        if (ngaySinh != null && ngaySinh.toString().trim() != "" && !isDate(ngaySinh.toString().trim())) {
            alert("Ngày sinh khách hàng thứ " + i + " không hợp lệ");
            return false;
        }
    }
    return true;
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};

function isDate(txtDate)
{
    var currVal = txtDate;
    if(currVal == '')
        return false;
     
    //Declare Regex 
    var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var dtArray = currVal.match(rxDatePattern); // is format OK?
    
    if (dtArray == null)
        return false;
    
    //Checks for mm/dd/yyyy format.
    dtMonth = dtArray[1];
    dtDay= dtArray[3];
    dtYear = dtArray[5];
    
    if (dtMonth < 1 || dtMonth > 12)
        return false;
    else if (dtDay < 1 || dtDay> 31)
        return false;
    else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
        return false;
    else if (dtMonth == 2)
    {
        var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
        if (dtDay> 29 || (dtDay ==29 && !isleap))
        return false;
    }
    return true;
}